﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BSPortal.Usercontrol
{
    public partial class Registry : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["Username"] !=null)
                Response.Redirect("trangchu.html");

            LoadCity();
            LoadDistrict();
            LoadWard();
        }

        protected  void LoadCity()
        {
            var mainService = Common.GetClientService();
            var listCity = mainService.GetAllCity().OrderBy(row=>row.Name);

            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "Name";
            ddlCity.DataSource = listCity;
            ddlCity.DataBind();

            ddlCity.SelectedValue = Common.GetValueInAppSetting("HCMCity_Id", "1");
        }

        protected void LoadDistrict()
        {
            if (ddlCity.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listDistrict = mainService.GetAllDistrict(int.Parse(ddlCity.SelectedValue)).OrderBy(row => row.Name);

                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataBind();
            }
        }

        protected void LoadWard()
        {
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listWard =
                    mainService.GetAllWard(int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue)).OrderBy(row => row.Name);

                ddlWard.DataValueField = "WardId";
                ddlWard.DataTextField = "Name";
                ddlWard.DataSource = listWard;
                ddlWard.DataBind();
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadWard();
        }

        protected void btnRegistry_Click(object sender, EventArgs e)
        {
            MainService mainService = Common.GetClientService();
            var user = mainService.GetUserById(txtUsername.Text);
            if(user!= null)
            {
                lblStatus.Text = "Username đã được đăng ký.";
            }

            int insert = mainService.CreateUser(txtUsername.Text, txtPassword.Text, txtName.Text, txtAddress.Text, txtEmail.Text, txtPhone.Text, 1, int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue), int.Parse(ddlWard.SelectedValue));
            if (insert == (int)Common.RetCode.SUCESS)
            {
                mainService.Login(txtUsername.Text, txtPassword.Text);
                Response.Redirect("trangchu.html");
            }
        }        
    }
}